#include "image.h"
#include<QObject>
image::image(QObject *parent) : QObject(parent)
{

}
QString image::getJsonValue(QString key,QByteArray replyData)
{
    QJsonParseError parseError;
    //1.先将返回的数据转换成json文件
    QJsonDocument jsonDocument=QJsonDocument::fromJson(replyData,&parseError);
    if(parseError.error==QJsonParseError::NoError)
    {
        //json文件对象是否存在
        if(jsonDocument.isObject())
        {
            //将json文件对象转换成json对象
            QJsonObject jsonObj=jsonDocument.object();
            if(jsonObj.contains(key))
            {
                QJsonValue jsonValue=jsonObj.value(key);
                if(jsonValue.isString())//第一次获取token是string
                {
                    return jsonValue.toString();
                }
                else if(jsonValue.isArray())//第二次是识别结果数组
                {
                    QJsonArray jsonArray=jsonValue.toArray();
                    QJsonValue result=jsonArray.at(0);
                    return result.toString();
                }
            }
        }
    }
    return "";
}
QString image::getAccessToken()
{
    //1.先获取aceess_token（把appId+sceretKey传过去）
    QString access_token;
    //先组合url
    QString tokenUrl=baidu_tokenUrl.arg(api_key).arg(secret_key);
    //封装好需要的数据
    QMap<QString,QString> header;
    header.insert(QString("Content-Type"),QString("application/json"));
    //发送的数据
    QByteArray requestData;
    //服务器返回的数据
    QByteArray replyData;
    //发送请求
    HttpRequest m_http;
    bool result=m_http.post_request(tokenUrl,header,requestData,replyData);
    if(result)
    {
        access_token=getJsonValue("access_token",replyData);
        return access_token;
    }
    else
    {
        QMessageBox::warning(NULL,"warning","error!");
    }
}
//获取task_id
QString image::submitRequest1(QString accessToken,QByteArray requestData)
{
    //1.先拼接url和body（图片和问题）
    QString imageUrl=image_Url1.arg(accessToken);
    QMap<QString,QString> header;
    header.insert(QString("Content-Type"),QString("application/json"));
    //服务器返回的数据
    QByteArray replyData;
    //发送请求
    HttpRequest m_http;
    bool result=m_http.post_request(imageUrl,header,requestData,replyData);
    if(result)
    {
        QJsonDocument doc = QJsonDocument::fromJson(replyData);
        QJsonObject obj = doc.object();
        QString taskId=obj.value("result").toObject().value("task_id").toString();
        qDebug()<<"result:"+taskId;
        return taskId;
    }
    else
    {
        QMessageBox::warning(NULL,"warning","error!");
    }
}
//动物识别
QString image::submitRequest2(QString accessToken,QString taskId)
{
    qDebug()<<accessToken;
    //1.先拼接url和header
    QString imageUrl=image_Url2.arg(accessToken);
    QMap<QString,QString> header;
    header.insert(QString("Content-Type"),QString("application/json"));
    //2.制作Body
    //把请求数据包装成一个Body
    QJsonObject requestData;
    requestData["task_id"]=taskId;
    QJsonDocument doc(requestData);
    QByteArray postData = doc.toJson(QJsonDocument::Compact);
    //服务器返回的数据
    QByteArray replyData;
    //3.发送请求
    HttpRequest m_http;
    bool result=m_http.post_request(imageUrl,header,postData,replyData);
    if(result)
    {
        //把返回的json数据转为json对象
        QJsonObject obj=QJsonDocument::fromJson(replyData).object();
        QJsonValue val=obj.value("error_msg").toString();
    }
    else
    {
        QMessageBox::warning(NULL,"warning","error!");
    }
}
//图像理解
QString image::submitRequest3(QString accessToken,QByteArray postData)
{
    //1.先拼接url和header
    QString imageUrl=image_Url3.arg(accessToken);
    QMap<QString,QString> header;
    header.insert(QString("Content-Type"),QString("application/json"));
    //服务器返回的数据
    QByteArray replyData;
    //3.发送请求
    HttpRequest m_http;
    bool result=m_http.post_request(imageUrl,header,postData,replyData);
    if(result)
    {
        QJsonObject obj=QJsonDocument::fromJson(replyData).object();
        QJsonValue val=obj.value("result");
        QJsonArray arr=val.toArray();
        QJsonValue ret=arr.at(0);
        QString name=ret.toObject().value("name").toString();
        return name;
    }
    else
    {
        QMessageBox::warning(NULL,"warning","error!");
    }
}
